<?php

// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------

/**
 * cat_user Database of the driver class.
 *
 * @Author:    <sanshi0815@tom.com>bruce(sanshi0815)
 * @Package:   Database Operate class of the system.
 * @Copyright: Copyright (c) 2005 - 2006 
 * @Version:   $Id: DB_cat_user.class.php 2009-09-03 13:54:29 $
 * @Show Note: 用户管理模块，用户信息以及权限，与用户中心通讯
 */
/* @Package of the database class */
include_once(INTERFACE_PATH."interfaceConfig.php");
Class DB_cat_user extends PdoMysql
{
    /* Mysql Table Name */
    public $tableName = "cat_user";

    /**
     * Construction this Database Object.
     *
     * @Author: bruce(sanshi0815).
     * @Show: Get the PdoMysql Object and Set the default Charset.
     * @Return: ..
     */
    public function __construct()
    {
    		parent::__construct();
			$this->user_manager = new server_user_manager();
    }

    /**
     * Execute the insert method.
     *
     * @Author: bruce(sanshi0815).
     * @Show: Execute 'insert' method by the PdoMysql Object.
     * @Param: $values Array -- Key is the Table Coulnum and Value is that values.
     * @Return: False OR int.
     */
	 /**
	 取得数据需要字段
	 */
	private function __get_db_values($values,$cat_center_uid)
	{
		$field_arr = array('cat_user_name','admin_group_id','cat_user_level');
		if(!empty($cat_center_uid))
			$temp['cat_center_uid'] = "{$cat_center_uid}";
		foreach($values as $k=>$v)
		{
			if(in_array($k,$field_arr))
			{
				$temp[$k] = $v;
			}
		}
		return $temp;
	}
	/**
	*取得用户中心注册字段
	*/
	private function __get_server_values($values)
	{
		$field_arr = array("admin_user_id","admin_user_name","admin_user_mail","admin_user_mobile","admin_user_pwd","admin_user_state","admin_user_sub_permission");
		$temp = array();
		foreach($values as $k=>$v)
		{
			if(in_array($k,$field_arr))
			{
				$temp[$k] = $v;
			}
		}
		return $temp;
	}
    public function insert_cat_user($values)
    {
		$temp = array();
		$temp = $this->__get_server_values($values);
		$cat_center_uid = $this->user_manager->register($temp);
		if(empty($cat_center_uid)) return -2001;
		if($cat_center_uid < 0 ) return $cat_center_uid;
		$temp = array();
		$temp = $this->__get_db_values($values,$cat_center_uid);
		//print_r($temp);exit();
        return $this->insert($this->tableName, $temp);
    }

    /**
     * Execute the update method.
     *
     * @Author: bruce(sanshi0815).
     * @Show: Execute 'update' method by the PdoMysql Object.
     * @Param: $values Array -- Key is the Table Coulnum and Value is that values.
     * @Param: @where  Array -- Key is the Table Coulnum and Value is the where values.
     * @Return: False OR int.
     */
    public function update_cat_user($values, $where)
    {
		//取得服务端跟新的字段
		$temp_ser = array();
		$temp_ser = $this->__get_server_values($values);
		if(!empty($temp_ser))
		{
			//根据where条件取得用户信息分别更新
			$temp = $this->getAll($where);
			foreach($temp as $v)
			{
				//跟新服务器的每条信息
				$cat_center_uid = $v['cat_center_uid'];
				$this->user_manager->change_user_info($cat_center_uid,$temp_ser);
			}
		}
		$temp = array();
		$temp = $this->__get_db_values($values,"");
		if(empty($temp)) return true;
        return $this->update($this->tableName, $temp, $where);
    }
    
    /**
     * Execute the delete method.
     *
     * @Author: bruce(sanshi0815).
     * @Show: Execute 'delete' method by the PdoMysql Object.
     * @Param: @where  Array -- Key is the Table Coulnum and Value is the where values.
     * @Return: False OR int.
     */
    public function delete_cat_user($where)
    {
        return $this->delete($this->tableName, $where);
    }

    /**
     * Execute the select method.
     *
     * @Author: bruce(sanshi0815)
     * @Show: Execute 'select' method by the PdoMysql Object, Return the item set.
     * @Param: $where String -- Search where.
     * @Param: $col String -- Search Order.
     * @Param: $numrows Int -- Search item num.
     * @Param: $offset Int -- Search item set offset.
     * @Param: $field String -- Search item name.
     * @Return: False OR Array.
     */
    public function getAll($where='1', $col='', $numrows=-1, $offset=-1, $field="*")
    {
        $sql = sprintf("SELECT %s FROM %s WHERE %s", $field, $this->tableName, $where);
        return $this->select($sql, $col, $numrows, $offset);
    }

    /**
     * Get one Item.
     *
     * @Author: bruce(sanshi0815)
     * @Show: Search the database.Return one item.
     * @Param: $index Array -- Key is the index Key.Value is the index value.
     * @Return: Array.
     */
    public function getOne($cat_center_uid)
    {
		$user_info = $this->user_manager->get_user_info($cat_center_uid);
		$sql = sprintf("SELECT * FROM %s WHERE cat_center_uid = '%s'", $this->tableName, $cat_center_uid);
		$result = $this->select($sql, 'cat_center_uid', 1, 0);
		$temp = isset($result[0]) ? $result[0] : array();
		return !empty($user_info)&&!empty($temp) ? array_merge($user_info,$temp):array();
    }
    
}

/* Public function is the Class method, Design by Hooker framework designer */
/* CreateTime: 2009-09-03 13:54:29 */

?>